package com.cn.algorithm01;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
 * Queue是一个接口，LinkedList实现了队列接口，队列先进先出
 * isEmpty 和 null 的区别：isEmpty是值是否为空。null是对象是否new。
 */
public class Code22_BinaryTreLevelorderTraversalli {
    // 创建一个二叉树节点对象
    public static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int v) {
            val = v;
        }
    }

    public List<List<Integer>> levelOrderBottom(TreeNode root){
        List<List<Integer>> ans = new LinkedList<List<Integer>>();
        if (root == null) {
            return ans;
        }

        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> curAns = new LinkedList<Integer>();
            for (int i = 0; i < size; i++) {
                TreeNode curNode = queue.poll();
                curAns.add(curNode.val);
                if (curNode.left != null) {
                    queue.add(curNode.left);
                }
                if (curNode.right != null) {
                    queue.offer(curNode.right);
                }
            }
            ans.add(0, curAns);
        }
        return ans;
    }

    public static void main(String[] args) {
        //Stack 栈的数据接口类似弹夹，先进后出
        Stack<Integer> stack = new Stack<Integer>();
        String h = null;
       /* System.out.println(h == null);
        System.out.println(stack.isEmpty());*/
        stack.add(1);
        stack.add(2);
        stack.add(3);
        while (!stack.isEmpty() ) {
            System.out.println(stack.pop());
        }

        // 通过LinkedList或者数组模仿栈，其中数组是最快的
        System.out.println("====");
        LinkedList<Integer> s = new LinkedList<Integer>();
        s.addLast(1);
        s.addLast(2);
        s.addLast(3);
        System.out.println(s.pollLast());
        System.out.println(s.pollLast());
        System.out.println(s.pollLast());

    }

}
